#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use tauri::{Manager, SystemTray, SystemTrayEvent};
use tauri_plugin_autostart::MacosLauncher;
use commands::*;

mod commands;
mod distro;

fn main() {
    let system_tray = SystemTray::new()
        .with_menu(
            tauri::Menu::new()
                .add_item(tauri::CustomMenuItem::new("show", "显示界面"))
                .add_item(tauri::CustomMenuItem::new("quit", "退出"))
        );

    tauri::Builder::default()
        .plugin(tauri_plugin_autostart::init(
            MacosLauncher::LaunchAgent,
            Some(vec!["--flag"]),
        ))
        .plugin(tauri_plugin_single_instance::init(|app, argv, cwd| {
            app.emit_all("single-instance", (argv, cwd)).unwrap();
        }))
        .system_tray(system_tray)
        .on_system_tray_event(|app, event| match event {
            SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
                "show" => {
                    let window = app.get_window("main").unwrap();
                    window.show().unwrap();
                    window.set_focus().unwrap();
                }
                "quit" => {
                    app.exit(0);
                }
                _ => {}
            },
            _ => {}
        })
        .invoke_handler(tauri::generate_handler![
            get_system_info,
            list_available_mirrors,
            switch_mirror,
            test_mirror_speed,
            rollback_config
        ])
        .run(tauri::generate_context!())
        .expect("运行应用程序时出错");
}